package com.android.job.service.request;

import java.util.ArrayList;

import org.apache.http.message.BasicNameValuePair;

import android.os.Handler;
import android.util.Log;

import com.android.job.fusion.FusionCode;
import com.android.job.service.ConnectionTask;
import com.android.job.service.JSONRequest;
import com.android.job.util.CommonUtil;
import com.android.job.util.json.JSONException;
import com.android.job.util.json.JSONObject;

/**
 * 版本更新请求
 * @author 
 *
 */
public class UpdateVerRequest extends JSONRequest
{
	private String version;
	private String clientType;

	public UpdateVerRequest(Handler handler, String httpUrl)
	{
		super(handler, httpUrl);
		setHttpType(ConnectionTask.POST);
	}

	public void setValues(String version, String clientType)
	{
		this.version = version;
		this.clientType = clientType;
	}

	@Override
	protected boolean parseJSONResponse(JSONObject json)
	{
		Log.i("UpdateVerRequest","===response===json======"+json.toString());
		if (!json.isNull("jsonData"))
		{
			JSONObject jsonObject;
			try
			{
				jsonObject = json.getJSONObject("jsonData");
				if (!jsonObject.isNull("update"))
				{
					String update = jsonObject.getString("update");
					String url = "";
					if ("1".equals(update))
					{
						if (!jsonObject.isNull("url"))
						{
							url = jsonObject.getString("url");
						}
					}

					CommonUtil.sendMessage(handler, FusionCode.RETURN_JSONOBJECT,
							url);
				}
			}
			catch (JSONException e)
			{
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

			
		}

		return true;
	}

	@Override
	protected void appendMainBody()
	{
		this.paramsters = new ArrayList();
		JSONObject verJson;
		try
		{
			jsonObject = new JSONObject();
			verJson = new JSONObject();

			verJson.put("version", version);
			verJson.put("clientType", clientType);

			jsonObject.put("requestType", "checkVersion");
			jsonObject.put("jsonData", verJson);
			paramsters.add(new BasicNameValuePair("jsonData", jsonObject.toString()));
			Log.i("UpdateVerRequest","===request===json======"+jsonObject.toString());
		}
		catch (JSONException e)
		{
			e.printStackTrace();
		}
	}

}
